import json

import pandas as pd
import os

path = './data'
fileName = "parks.txt"


def RequestParks(useCache):
    if not os.path.exists(path):
        os.mkdir(path)
    _fileName = "{}/{}".format(path, fileName)

    fileExist = os.path.exists(_fileName)
    if not useCache or not fileExist:
        print("use net data")
        url = 'http://www.jjjlyykt.com/nkjq/jingqumulu/2021/0909/434.html'

        table = pd.read_html(url)
        pd.set_option('display.max_rows', None)  # 显示全部的行

        df = table[0]

        # print(df.columns)
        # print(str(df))
        df.rename(columns={0: "id", 1: 'name', 2: "price", 3: 'discount_num', 4: 'detail'}, inplace=True)
        # print("----------------------------------------------")
        # print(df.to_json(force_ascii=False,lines=True,orient= "records"))
        _json = df.to_json(force_ascii=False, orient="records")
        with open(_fileName, "wt", encoding='utf8') as out_file:  # 保存为txt文件
            out_file.write(_json)
        return _json
    else:
        print("use cache")
        with open(_fileName, 'rt', encoding='utf8') as read_file:
            return read_file.read()